package com.hxzy.controller.hsbuilding;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hxzy.common.ResultAjax;
import com.hxzy.util.StringUtil;
import com.hxzy.util.ThreadLocalUtil;
import com.hxzy.common.BaseServlet;
import com.hxzy.dto.BuildDTO;
import com.hxzy.entity.Build;
import com.hxzy.service.BuildService;
import com.hxzy.service.impl.BuildServiceImpl;
import com.hxzy.vo.BuildVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/3/8 14:32
 */

@WebServlet(urlPatterns = "/build/search")
public class BuildServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //当前用户是谁
        ThreadLocalUtil.threadLocal.get();
        //开启分页
        super.startPage(req);
       try {
           String id = req.getParameter(UUID.randomUUID().toString());
           if (StringUtil.isNotBlank(id)){
               ResultAjax error = ResultAjax.error("id为空");
               super.render(resp, JSONObject.toJSONString(error));
           }
           //收集数据
           BuildDTO buildDTO = super.CollectiongParameterVlues(req, BuildDTO.class);
           //查询数据
           BuildService service=new BuildServiceImpl();
           List<BuildVo> search = service.search(buildDTO);

           //返回客服端
           // 两种方法 1:ResultAjax.success_page() 加上super.render(resp, JSONObject.toJSONString(resultAjax));
//           ResultAjax resultAjax = ResultAjax.success_page(new PageInfo(search));
//           super.render(resp, JSONObject.toJSONString(resultAjax));

           // 2
           super.render(resp, new PageInfo(search));

       } catch (IOException e) {
           e.printStackTrace();
       } catch (InstantiationException e) {
           e.printStackTrace();
       } catch (IllegalAccessException e) {
           e.printStackTrace();
       }

    }
}
